﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using System.Collections.Generic;

namespace FI.Core.IoC
{
    /// <summary>
    /// A dictionary of object can be activated by service locator.
    /// </summary>
    /// <typeparam name="T">The type of element's value.</typeparam>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
    public class ServiceLocatableDictionary<T>
    {
        #region Properties
        /// <summary>
        /// Gets the service locator.
        /// </summary>
        /// <value>
        /// The service locator.
        /// </value>
        public IServiceLocator ServiceLocator { get; private set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceLocatableDictionary{T}"/> class.
        /// </summary>
        /// <param name="serviceLocatorName">Name of the service locator.</param>
        public ServiceLocatableDictionary(string serviceLocatorName = null)
        {
            this.ServiceLocator = ServiceLocatorFactory.GetServiceLocator(serviceLocatorName);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the keys.
        /// </summary>
        /// <value>
        /// The keys.
        /// </value>
        public IEnumerable<string> Keys
        {
            get { return this.ServiceLocator.GetAllKeys<T>(); }
        }

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <value>
        /// The values.
        /// </value>
        public IEnumerable<T> Values
        {
            get { return this.ServiceLocator.GetAllServices<T>(); }
        }
        #endregion

        #region Index
        /// <summary>
        /// Gets the object with the specified key.
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        /// <param name="key">The key.</param>
        /// <returns>The value.</returns>
        public T this[string key]
        {
            get { return this.ServiceLocator.GetService<T>(key); }
        }
        #endregion
    }
}
